package com.springsource.mvc.ReturnValueHandler;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.*;

import java.util.List;

@Slf4j
public class Main {

    public static void main(String[] args) throws Exception{

    }

    public static HandlerMethodReturnValueHandlerComposite  getHandlerMethodReturnValueHandlerComposite() {
        HandlerMethodReturnValueHandlerComposite composite = new HandlerMethodReturnValueHandlerComposite();
        composite.addHandlers(
                List.of(                new ModelAndViewMethodReturnValueHandler(),
                        new ViewNameMethodReturnValueHandler(),
                        new ServletModelAttributeMethodProcessor(false),
                        new HttpEntityMethodProcessor(List.of(new MappingJackson2HttpMessageConverter())),
                        new HttpHeadersReturnValueHandler(),
                        new RequestResponseBodyMethodProcessor(List.of(new MappingJackson2HttpMessageConverter())),
                        new ServletModelAttributeMethodProcessor(true))
        );
        return composite;
    }

    private static void renderView(AnnotationConfigApplicationContext context, ModelAndViewContainer container,
                                   ServletWebRequest request) throws Exception {

    }

    @Slf4j
    static class Controller {

        public ModelAndView test1() {
            log.warn("test1()");
            ModelAndView mav = new ModelAndView("view1");
            mav.addObject("name", "张三");
            return mav;
        }

        public String test2() {
            log.warn("test2()");
            return "view2";
        }

        @ModelAttribute
        public User test3() {
            log.warn("test3()");
            return new User("李四", 20);
        }

        public User test4() {
            log.warn("test4()");
            return new User("王五", 30);
        }

        public HttpEntity<User> test5() {
            log.warn("test5()");
            return new HttpEntity<User>(new User("赵六", 40));
        }

        public HttpHeaders  test6() {
            log.warn("test6()");
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "text/html");
            return headers;
        }

        @ResponseBody
        public User  test7() {
            log.warn("test7()");
            return new User("孙七", 50);
        }


    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class User {
        private String name;
        private int age;
    }
}
